using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0003. 无重复字符的最长子串")]
    public class No0003_LengthOfLongestSubstring : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(LengthOfLongestSubstring, (string)null, 0);
            Assert.TestExe(LengthOfLongestSubstring, "1", 1);
            Assert.TestExe(LengthOfLongestSubstring, "bbbbb", 1);
            Assert.TestExe(LengthOfLongestSubstring, "pwwkew", 3);
        }

        public int LengthOfLongestSubstring(string s)
        {
            if (s == null)
            {
                return 0;
            }
            Dictionary<char, int> dic = new Dictionary<char, int>();
            int n = s.Length;
            int rk = -1, ans = 0;
            for (int i = 0; i < n; i++)
            {
                if (i != 0)
                {
                    dic.Remove(s[i - 1]);
                }
                while (rk + 1 < n && !dic.ContainsKey(s[rk + 1]))
                {
                    dic.Add(s[rk + 1], 0);
                    rk += 1;
                }
                ans = Math.Max(ans, rk - i + 1);
            }
            return ans;
        }
    }
}
